<template>
    <div></div>
</template>

<script>
  import {getUrlParam} from "../api/params";
  import weixinApi from "../api/weixin";
  import {setUser} from "../utils/auth";

  export default {
    mounted() {
      let code = getUrlParam('code')
      if (code !== null) {
        //如果是微信登陆,根据code获取access_token
        weixinApi.getAccessToken(code).then(res=>{
          let access_token = res.data.access_token
          let openid = res.data.openid
          console.log('access_token:'+access_token+'openid:'+openid)
          weixinApi.getUserInfo(access_token,openid).then(res2=>{
            let nickname = res2.data.nickname
            let headimgurl = res2.data.headimgurl
            setUser(access_token,nickname,headimgurl)
            location.href="/"
          })
        })
      }
    },
    name: "weixinlogin"
    }
</script>

<style scoped>

</style>
